
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-01-14 14:46:37                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名： ManagerTokenService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using AutoMapper;
using BestTrader.Cms.Core.Extensions;
using BestTrader.Cms.Core.Helper;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Models;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.ViewModels.Admin;
using System;
using System.Collections.Generic;
using System.Text;

namespace BestTrader.Cms.Services
{
    public class ManagerTokenService : IManagerTokenService
    {
        private readonly IManagerTokenRepository _repository;
        private readonly IMapper _mapper;

        public ManagerTokenService(IManagerTokenRepository repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }

        public TableDataResult<String> AddOrModify(Int32 managerId)
        {
            var result = new TableDataResult<String>();

            //TODO ADD
            var token = MD5EncryptHelper.Get32MD5One(DateTime.Now.ToString($"{managerId}_yyyyMMddHHmmssfff"));
            ManagerToken managerToken = new ManagerToken
            {
                ManagerId = managerId,
                Token = token,
                IsUsed = false,
                AddManagerId = managerId,
                AddTime = DateTime.Now
            };

            var rows = _repository.Insert(managerToken);
            if (rows > 0)
            {
                result.Data = token;
                result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
            }

            return result;
        }
        /// <summary>
        /// 验证注册码
        /// </summary>
        /// <returns></returns>
        public bool ValidRegisterToken(string UID, string Reg)
        {
            bool result = true;
            var parentManagerId = DESEncryptHelper.Decrypt(UID).ToInt32() ?? 0;

            var _token = _repository.Get($"select * from ManagerToken where ManagerId='{ parentManagerId }' and Token= '{ Reg }'");

            if (_token == null)
            {
                result= false;
                // result.ResultCode = ResultCodeAddMsgKeys.RegisterTokenExceptionCode;
                //result.ResultMsg = ResultCodeAddMsgKeys.RegisterTokenExceptionMsg;
            }
            else if (_token.IsUsed.HasValue && _token.IsUsed.Value)
            {
                result = false;
                // result.ResultCode = ResultCodeAddMsgKeys.RegisterTokenExceptionCode;
                // result.ResultMsg = ResultCodeAddMsgKeys.RegisterTokenExceptionMsg;
            }

            return result;


        }


    }
}